package bairui.international.weixin.portal.miniapp.controller.order.product;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_ElectronicTicket;
import bairui.international.model.domain.jpa.TB_GoodsInfo;
import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.domain.jpa.TB_ProductOrderItem;
import bairui.international.model.status.State_ProductOrder;
import bairui.international.model.type.Type_ProductOrder;
import bairui.international.model.type.Type_ProductOrderPay;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_ProductOrderItem;
import bairui.international.weixin.portal.miniapp.controller.product.product.C_Product_ShowImg_Get;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 得到订单
 *
 * @generated
 * 
 * @see STest_C_Order_Product_Get_Get 测试
 *
 */
@Controller
public class C_Order_Product_Get_Get
 extends SC_Order_Product_Get_Get 
{
	
	@Autowired private Service_ProductOrder service_ProductOrder;
	@Autowired private Service_ProductOrderItem service_ProductOrderItem;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request, 
		@PathVariable(name=Get.PathVariableName.Id) long id,
		@RequestParam(required=false) Boolean showProduction
	) throws Exception {
		TB_ProductOrder order = service_ProductOrder.findById(id);
		Type_ProductOrderPay way = order.getPayWay();
		Double pay = order.getPay();
		pay = (null == pay) ? 0 : pay;
		TB_GoodsInfo g = order.getGoodsInfo();
		// 抵用券
		TB_ElectronicTicket ticket = order.getElectronicTicket();
		Double ticketPrice = null != ticket ? ticket.getElectronicTicketOrder().getPrice() : 0;
		// 订单项
		List<ProductOutput> orderItems = new ArrayList<>();
		if(null != showProduction && true == showProduction){
			orderItems = service_ProductOrderItem
					.list(it->it.productOrder.eq(order))
					.stream()
					.map(it->new ProductOutput(it))
					.collect(Collectors.toList());
		}
		//
		return new Output(
				order.getId(), 
				order.getNumber(),
				order.getPrice(),
				new C_Order_Product_ShowImg_Get.Get.Operate() {
					@Override public Object getId() {
						return order.getId();
					}
				}.getUrl(),
				order.getState(),
				order.getState().getComment(),
				order.getType(),
				order.getType().getComment(),
				order.getComment(),
				//
				null==g ? null : order.getArea().getId(),
				null==g ? "" : g.getName(),
				null==g ? "" : g.getPhone(),
				null==g ? "" : g.getAddress(),
				null==g ? "" : g.getFixAddress(),
				//
				order.getUsedScore(),
				(way != null && way == Type_ProductOrderPay.OnLine) ? pay : 0,
				(way != null && way == Type_ProductOrderPay.Goods) ? pay : 0,
				ticketPrice,
				orderItems
			);
	}
	
	@AllArgsConstructor
	private class Output {
		private @Getter Long id;
		private @Getter String number;
		private @Getter Double price;
		private @Getter String imgUrl;
		private @Getter State_ProductOrder state;
		private @Getter String stateComment;
		private @Getter Type_ProductOrder type;
		private @Getter String typeComment;
		private @Getter String comment;
		//
		private @Getter Long townId;
		private @Getter String username;
		private @Getter String phone;
		private @Getter String address;
		private @Getter String fixAddress;
		// 支付方式
		private @Getter Double scorePrice;		// 余额支付
		private @Getter Double onlinePrice; 	// 在线支付
		private @Getter Double underlinePrice;	// 线下支付
		private @Getter Double ticketPrice;		// 抵用券
		// 产品
		private @Getter List<ProductOutput> items;
	}
	
	@AllArgsConstructor
	private static class ProductOutput{
		@Getter private Long id;
		@Getter private String name;
		@Getter private String comment;
		@Getter private Double perPrice;
		@Getter private Double totalPrice;
		@Getter private Long amount;
		@Getter private String imgUrl;
		@Getter private String unit;
		
		public ProductOutput(TB_ProductOrderItem orderItem) {
			this.id = orderItem.getProduct().getId();
			this.name = orderItem.getProductName();
			this.comment = null == orderItem.getProductComment() ? "" : orderItem.getProductComment();
			this.amount = orderItem.getAmount();
			this.perPrice = orderItem.getProductPerPrice();
			this.totalPrice = orderItem.getProductTotalPrice();
			this.imgUrl = new C_Product_ShowImg_Get.Get.Operate() {
				@Override public Object getId() {
					return orderItem.getProduct().getId();
				}
			}.getUrl();
			//
			this.unit = null == orderItem.getProductUnit() ? "" : orderItem.getProductUnit();
		}
		
	}
	
}
